package com.cdtu.store.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.store.common.entity.SpecParamEntity;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.Query;
import com.cdtu.store.item.dao.SpecParamDao;
import com.cdtu.store.item.service.SpecParamService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;



@Service("specParamService")
public class SpecParamServiceImpl extends ServiceImpl<SpecParamDao, SpecParamEntity> implements SpecParamService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecParamEntity> page = this.page(
                new Query<SpecParamEntity>().getPage(params),
                new QueryWrapper<SpecParamEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cid) {
        if (cid == 0) {
            IPage<SpecParamEntity> page = this.page(new Query<SpecParamEntity>().getPage(params),
                    new QueryWrapper<SpecParamEntity>()
            );
            return new PageUtils(page);
        } else {
            String key = (String) params.get("key");
            QueryWrapper<SpecParamEntity> wrapper = new QueryWrapper<SpecParamEntity>().eq("cid", cid);
            if (!StringUtils.isEmpty(key)) {
                wrapper.and((obj) -> {
                    obj.eq("id",key)
                            .or().like("name",key)
                            .or().like("groupName",key);
                });
            }
            IPage<SpecParamEntity> page = this.page(new Query<SpecParamEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public void updateParam(Long id, String name) {
        SpecParamEntity specParamEntity = new SpecParamEntity();
        specParamEntity.setGroupId(id);
        specParamEntity.setGroupName(name);
        this.update(specParamEntity,new UpdateWrapper<SpecParamEntity>().eq("group_id",id));
    }

    @Override
    public List<SpecParamEntity> BaseList(Long cid) {
        List<SpecParamEntity> list = this.list(new QueryWrapper<SpecParamEntity>().eq("generic", 1).eq("cid", cid));
        return list;

    }

    @Override
    public List<SpecParamEntity> SaleList(Long cid) {
        List<SpecParamEntity> list = this.list(new QueryWrapper<SpecParamEntity>().eq("generic", 0).eq("cid", cid));
        return list;
    }

    @Override
    public List<SpecParamEntity> queryParams(Long gid, Long cid, Integer generic, Integer searching) {
        QueryWrapper<SpecParamEntity> wrapper = new QueryWrapper<>();
        if(gid != null){
            wrapper.eq("group_id", gid);
        }
        if(cid != null){
            wrapper.eq("cid", cid);
        }
        if(generic != null){
            wrapper.eq("generic", generic);
        }
        if(searching != null){
            wrapper.eq("searching", searching);
        }
        return this.list(wrapper);
    }
}